package cease.butter;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import net.sf.json.JSON;

import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import cease.urs.LoginException;
import cease.urs.Loginer;
import cease.urs.Session;
import cease.vo.Butter;

/**
 * @author dhf
 */
public class FavoriteExecutor extends BaseExecutor {
    private static FavoriteExecutor executor = new FavoriteExecutor();

    public static FavoriteExecutor getInstance() {
        return executor;
    }

    public Collection<Butter> show(Session session, String user, Integer size,
            String cursor, String sort) throws ApiException {
        if (null != size) {
            if (size <= 0) {
                size = 1;
            }
            if (size > 100) {
                size = 100;
            }
        }
        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        if (null != user) {
            pairs.add(new BasicNameValuePair("id", user));
        }
        if (null != size) {
            pairs.add(new BasicNameValuePair("size", size + ""));
        }
        if (null != cursor) {
            pairs.add(new BasicNameValuePair("cursor", cursor));
        }
        if (null != sort) {
            pairs.add(new BasicNameValuePair("sort", sort));
        }
        pairs.add(new BasicNameValuePair("t", System.currentTimeMillis() + ""));
        JSON json = get(Constants.URL_FAVORITE_SHOW, pairs, null, session);
        return toButterArray(json);
    }

    public Butter add(Session session, String butterId) throws ApiException {
        if (StringUtils.isBlank(butterId)) {
            throw new IllegalArgumentException("[butterId] could not be null");
        }

        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        pairs.add(new BasicNameValuePair("id", butterId));
        JSON json = post(Constants.URL_FAVORITE_ADD, pairs, null, session);
        return toButter(json);
    }

    public Butter delete(Session session, String butterId) throws ApiException {
        if (StringUtils.isBlank(butterId)) {
            throw new IllegalArgumentException("[butterId] could not be null");
        }

        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        pairs.add(new BasicNameValuePair("id", butterId));
        JSON json = post(Constants.URL_FAVORITE_DELETE, pairs, null, session);
        return toButter(json);
    }

    public static void main(String[] args) throws LoginException, ApiException {
        Session session = Loginer.login("dhf@126.com", "wrongpassword");
        // 添加收藏
        System.out.println(executor.add(session, "9151560515321115185"));
        // 显示收藏
        System.out.println(executor.show(session, "dhf@163.com", null, null,
                null));
        // 删除收藏
        System.out.println(executor.delete(session, "9151560515321115185"));
    }
}
